package com.fms.trollalarm.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fms.trollalarm.R;
import com.fms.trollalarm.custom.MoveAnimation;
import com.fms.trollalarm.custom.MoveAnimation.OnMoveAnimationListener;
import com.fms.trollalarm.object.LevelType;
import com.fms.trollalarm.object.TrollType;
import com.fms.trollalarm.utils.Common;
import com.fms.trollalarm.utils.LevelSetting.Easy;
import com.fms.trollalarm.utils.LevelSetting.Hard;
import com.fms.trollalarm.utils.LevelSetting.Medium;

public class FindMeFragment extends AlarmFragment {

	private TextView tvTitle;
	private Button btSnooze;
	private RelativeLayout rlContent;
	private Button btChange;
	private long speed = 700;
	private long visibleTime = 3 * speed;
	private List<MoveAnimation> moveAnimations = new ArrayList<MoveAnimation>();

	public static FindMeFragment newInstance(int levelType) {
		FindMeFragment fragment = new FindMeFragment();
		Bundle bundle = new Bundle();
		bundle.putInt(Common.IntentExtra.TROLL_LEVEL, levelType);
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_find_me, null);
		findViewIDs(v);
		init();
		setEventView();
		return v;
	}

	private void setEventView() {
		btSnooze.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setOnSnoozeAlarm();
			}
		});
		
		btChange.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				setOnChangeTrollType(TrollType.FIND_ME);
			}
		});

		rlContent.getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {

					@Override
					public void onGlobalLayout() {
						rlContent.getViewTreeObserver()
								.removeGlobalOnLayoutListener(this);
						createDismissButton();
						for (int i = 0; i < 10; i++) {
							createFakeDismissButton();
						}
					}
				});
	}

	private void createFakeDismissButton() {
		final ImageView view = new ImageView(getActivity());
		view.setImageResource(R.drawable.ic_launcher);
		// button.setText("Dismiss");

		// RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
		// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.d("fms", "click dismiss");
				restartMove();
				createFakeDismissButton();
			}
		});
		
		rlContent.addView(view);

		view.getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {

					@Override
					public void onGlobalLayout() {
						view.getViewTreeObserver()
								.removeGlobalOnLayoutListener(this);
						MoveAnimation moveBt = new MoveAnimation(view,
								rlContent, getActivity());
						moveBt.start(speed, 5);
						moveBt.setOnMoveAnimationListener(new OnMoveAnimationListener() {

							@Override
							public void onBegin(View v) {
								v.setEnabled(false);

							}

							@Override
							public void onStop(View v) {
								v.setEnabled(true);
							}
						});
						moveAnimations.add(moveBt);
					}
				});

	}

	private void createDismissButton() {
		final ImageView view = new ImageView(getActivity());
		view.setImageResource(R.drawable.troll);
		view.postDelayed(new Runnable() {

			@Override
			public void run() {
				view.setImageResource(R.drawable.ic_launcher);
			}
		}, visibleTime);
		// RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
		// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setOnStopAlarm();
			}
		});
		view.bringToFront();
		rlContent.addView(view);

		view.getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {

					@Override
					public void onGlobalLayout() {
						view.getViewTreeObserver()
								.removeGlobalOnLayoutListener(this);
						MoveAnimation moveBt = new MoveAnimation(view,
								rlContent, getActivity());
						moveBt.start(speed, 5);
						moveBt.setOnMoveAnimationListener(new OnMoveAnimationListener() {

							@Override
							public void onBegin(View v) {
								v.setEnabled(false);
								view.setImageResource(R.drawable.troll);
								view.postDelayed(new Runnable() {

									@Override
									public void run() {
										view.setImageResource(R.drawable.ic_launcher);
									}
								}, visibleTime);
							}

							@Override
							public void onStop(View v) {
								v.setEnabled(true);
							}
						});
						moveAnimations.add(moveBt);
					}
				});

	}

	private void init() {
		int trollLevel = getTrollLevel();
		if (trollLevel == LevelType.EASY.ordinal()) {
			speed = Easy.FIND_ME_SPEED;
		} else if (trollLevel == LevelType.MEDIUM.ordinal()) {
			speed = Medium.FIND_ME_SPEED;
		} else {
			speed = Hard.FIND_ME_SPEED;
		}
		visibleTime = 3 * speed;
	}

	private void findViewIDs(View v) {
		tvTitle = (TextView) v.findViewById(R.id.tv_title);
		btSnooze = (Button) v.findViewById(R.id.bt_snooze);
		rlContent = (RelativeLayout) v.findViewById(R.id.rl_content);
		btChange = (Button)v.findViewById( R.id.bt_change );
	}

	public void restartMove() {
		moveAnimations.get(0).getView().bringToFront();
		for (int i = 0; i < moveAnimations.size(); i++) {
			moveAnimations.get(i).start(speed, 5);
		}
	}
}
